package com.lyp.supermanage.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lyp.supermanage.R;
import com.lyp.supermanage.api.Api;
import com.lyp.supermanage.bean.LoginBean;
import com.lyp.supermanage.utils.ProgressDialogUtils;


import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import pl.droidsonroids.gif.GifImageView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class LoginActivity extends Activity implements View.OnClickListener{

    private GifImageView mLoginimg;
    private EditText mUserName;
    private EditText mUserPwd;
    private Button mLogin;
    private String userName;
    private String userPwd;
    SPUtils sputils;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        sputils = new SPUtils("login");
        initview();
        mUserName.setText(sputils.getString("name",""));
        //暂时记住密码
        mUserPwd.setText(sputils.getString("pwd",""));
        start();
    }

    private void initview(){
        mLoginimg = (GifImageView) findViewById(R.id.start_loginimg);
        mUserName = (EditText) findViewById(R.id.et_loginuserName);
        mUserPwd = (EditText) findViewById(R.id.et_loginuserpwd);
        mLogin = (Button) findViewById(R.id.btn_logins);
        mUserName.setOnClickListener(this);
        mUserPwd.setOnClickListener(this);
        mLogin.setOnClickListener(this);
//        Glide.with(LoggingActivity.this).load(R.drawable.gif)
//                .diskCacheStrategy(DiskCacheStrategy.ALL)
//                .into(mLoginimg);
    }

    //启动图
    private void start(){
        /**
         //		 * 五秒后隐藏启动图/显示登陆页
         //		 */
        int status = getIntent().getIntExtra("status", 0);
        if (status == 1) {
            mLoginimg.setVisibility(View.GONE);
        } else {
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    mLoginimg.setVisibility(View.GONE);
                }
            }, 1000);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_logins:
                if (isNull()) {
                    // 判断网络是否连接并且是否可用
                    if (NetworkUtils.isConnected() && NetworkUtils.isAvailableByPing()) {
                        login();
                    } else {
                        ToastUtils.showShortSafe("当前网络不可用！");
                    }
                 }
                break;
        }
    }


    public void login(){
        ProgressDialogUtils.getInstance().builder(this).setCancelable(false).setCanCelMessage("登录已取消").show("正在登录中");
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Api.APP_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Api api = retrofit.create(Api.class);
        Call<LoginBean> call = api.getLogin(userName, userPwd);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                LoginBean bean = response.body();
                if (bean.getResult() == 1) {
                    ToastUtils.showShort(response.body().getMessage());
                    List<LoginBean.DataBean> dataBeen = bean.getData();
                    sputils.put("name", dataBeen.get(0).getUser());
                    //暂时记住密码
                    sputils.put("pwd", dataBeen.get(0).getPwd());
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    Bundle args = new Bundle();
                    args.putSerializable("dataBean", dataBeen.get(0));
                    intent.putExtras(args);
                    startActivity(intent);
                    finish();
                }
                else {
                    ToastUtils.showShort("登录失败！请检查用户名或密码是否正确！");
                }
                ProgressDialogUtils.getInstance().dismiss();
            }
            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                ToastUtils.showShort(t.getMessage());
            }
        });
    }

    //判断用户名和密码是否为空
    boolean isNull(){
        userName = mUserName.getText().toString();
        userPwd = mUserPwd.getText().toString();
        //判断用户名是否为空
        if (userName.isEmpty()){
            ToastUtils.showShort("请输入用户名");
            return false;
        } else if(userPwd.isEmpty()){ //判断密码是否为空
            ToastUtils.showShort("请输入密码");
            return false;
        }
        return true;
    }


}
